/* 测试消息转发子服务的客户端 */
#include <gflags/gflags.h>
#include <brpc/channel.h>
#include <thread>
#include <gtest/gtest.h>
#include "logger.hpp"
#include "brpc.hpp"
#include "etcd.hpp"
#include "base.pb.h"
#include "user.pb.h"
#include "utils.hpp"

DEFINE_bool(run_mode, false, "程序的运行模式, false-调试; true-发布");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(etcd_host, "127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(user_service, "/service/user_service", "调用的服务目录");

ChatSystem::ServiceManager::ptr service_manager;


void registry_user(const std::string& nickname)
{
    auto channel = service_manager->choose(FLAGS_user_service);
    ASSERT_TRUE(channel);

    //发起User Rpc调用
    // 2.实例化pb中的UserService_Stub类对象；
    ChatSystem::UserService_Stub stub(channel.get());
    // 3.发起rpc请求，获取响应进行处理；
    brpc::Controller cntl;
    ChatSystem::UserRegisterReq req;
    ChatSystem::UserRegisterRsp rsp;
    req.set_request_id(ChatSystem::Util::uuid());
    req.set_nickname(nickname);
    req.set_password("123456");

    stub.UserRegister(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

void set_user_avatar(const std::string &uid, const std::string &avatar) {
    auto channel = service_manager->choose(FLAGS_user_service);//获取通信信道
    ASSERT_TRUE(channel);
    ChatSystem::SetUserAvatarReq req;
    req.set_request_id(ChatSystem::Util::uuid());
    req.set_user_id(uid);
    req.set_login_session_id("login1");
    req.set_avatar(avatar);
    ChatSystem::SetUserAvatarRsp rsp;
    brpc::Controller cntl;
    ChatSystem::UserService_Stub stub(channel.get());
    stub.SetUserAvatar(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    google::ParseCommandLineFlags(&argc, &argv, true);
    ChatSystem::InitLogger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    //1. 先去注册中心进行服务的发现---发现user_server的服务器节点地址信息并实例化通信信道
    //1.1 先构造Rpc整体的服务管理信道类
    service_manager = std::make_shared<ChatSystem::ServiceManager>();
    service_manager->declared(FLAGS_user_service);  //设置需要关心的服务
    auto put_cb = std::bind(&ChatSystem::ServiceManager::onServiceOnline, service_manager.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&ChatSystem::ServiceManager::onServiceOffline, service_manager.get(), std::placeholders::_1, std::placeholders::_2);
    //1.2 构造服务发现对象 
    ChatSystem::Discovery::ptr dclient = std::make_shared<ChatSystem::Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);

    registry_user("猪爸爸");
    registry_user("猪妈妈");
    registry_user("小猪乔治");
    registry_user("小猪佩奇");

    std::string uid1, uid2, uid3, uid4;
    std::cout << "输入猪爸爸用户ID：";
    std::fflush(stdout);
    std::cin >> uid1;
    std::cout << "输入猪妈妈用户ID：";
    std::fflush(stdout);
    std::cin >> uid2;
    std::cout << "输入小猪乔治用户ID：";
    std::fflush(stdout);
    std::cin >> uid3;
    std::cout << "输入小猪佩奇用户ID：";
    std::fflush(stdout);
    std::cin >> uid4;
    set_user_avatar(uid1, "猪爸爸的头像数据");
    set_user_avatar(uid2, "猪妈妈的头像数据");
    set_user_avatar(uid3, "小猪乔治的头像数据");
    set_user_avatar(uid4, "小猪佩奇的头像数据");

    return 0;
}
